package com.example.agricultural_big_data.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.agricultural_big_data.pojo.User;
import com.example.agricultural_big_data.service.UserService;
import com.example.agricultural_big_data.util.AjaxResult;
import com.example.agricultural_big_data.util.LoginRequired;
import com.example.agricultural_big_data.util.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

/**
 * @author xiao yang
 * @Description: 用户登录
 * @date 2021/1/3113:22
 */
@RestController
@CrossOrigin
@RequestMapping("/user")
public class LoginController extends BaseController {
    @Autowired
    UserService userservice;


    @LoginRequired
    @PostMapping("/login")
    public Object loginByUsername(@RequestBody JSONObject data) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        return userservice.userLogin(data);
    }
    @LoginRequired
    @GetMapping("/info")
    public Object getInfoById(String userId){
        return userservice.getInfoById(userId);
    }

//    @LoginRequired
    @GetMapping("/list")
    public TableDataInfo list(User user){
        return getDataTable(userservice.getUserList(user));
    }

    @PostMapping("/add")
    public AjaxResult add(@RequestBody User user) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        return toAjax(userservice.addUser(user));
    }


}
